import ResizeObserver from 'resize-observer-polyfill'

const syncVar = (node, binding, vnode) => {
  if (binding.arg) {
    binding.value[binding.arg] = node.offsetWidth < node.scrollWidth
  } else {
    vnode.context[binding.expression] = node.offsetWidth < node.scrollWidth
  }
}
/**
 * 实时检测是否出现省略号
 */
export default {
  inserted(el, binding, vnode) {
    const node = binding.modifiers.parent ? el.parentNode : el
    syncVar(node, binding, vnode)

    const ro = new ResizeObserver((entries, observer) => {
      entries[0].target && syncVar(node, binding, vnode)
    })
    ro.observe(node)
  },
  componentUpdated(el, binding, vnode) {
    const node = binding.modifiers.parent ? el.parentNode : el
    syncVar(node, binding, vnode)
  }
}
